﻿using System;
using System.Linq;
using System.Collections.Generic;
using WebChat.Models;
using System.Web.Mvc;

namespace WebChat.Models 
{
     

    public class ChannelRepository
    {
        private ChannelDataClassesDataContext db;
        public IEnumerable<SelectListItem> UserList{get; set;}
        public IEnumerable<string> CategoriesSelected { get; set; }
        public ChannelRepository()
        {
            db = new ChannelDataClassesDataContext();
        }

        public IQueryable<Chatroom> showAllChatRooms()
        {
            return db.Chatrooms;
        }

        public Chatroom showChatroom(string name)
        {
            Chatroom room = (from o in db.Chatrooms
                             where o.Name == name
                             select o).FirstOrDefault();
            return room;
            
        }

        public void addChannel(Chatroom newChatRoom) 
        {
            db.Chatrooms.InsertOnSubmit(newChatRoom);
            db.SubmitChanges();
        }

        public Guid getId(string name) 
        {
            aspnet_User user = (from o in db.aspnet_Users
                                where o.UserName == name
                                select o).FirstOrDefault();
            return user.UserId;
        }

        public void updateChannel(Chatroom room)
        {
            db.SubmitChanges();
        }

        public List<aspnet_User> showAllUsers()
        {
            List<aspnet_User> users = (from u in db.aspnet_Users
                                       select u).ToList();
            return users;
        }

        public void addPermittedUsers(Permitted_user user) 
        {
                db.Permitted_users.InsertOnSubmit(user);
                db.SubmitChanges();
        }

    }
}

